from django.core.paginator import Paginator
from django.db.models.fields.json import JSONIContains
from django.http import JsonResponse
from django.shortcuts import render

from lianxi20250923.models import micorservice


# Create your views here.
def get_micorservice(request):
    data=micorservice.objects.all()
    token=request.META.get('HTTP_TOKEN')
    if not token:
        return JsonResponse(
            {
                'msg':'请登录'
            }
        )

    l=[]
    servicedname=request.GET.get('servicedname')
    protocol=request.GET.get('protocol')
    if servicedname:
        data = data.filter(servicename__icontains=servicedname)
    if protocol:
        data=data.filter(protocol__icontains=protocol)
    page = request.GET.get('page',1)
    size = request.GET.get('size') or 10
    data=Paginator(data,size).get_page(page)
    for i in data:
        l.append(
            {
                'servicename':i.servicename,
                'protocol':i.protocol,
                'port':i.port,
                'apinumber':i.apinumber,
                'person':i.person,
                'desc':i.desc,
                'createtime':i.createtime,
                'modyfitime':i.modifytime
            }
        )
    return JsonResponse(
        {
            'msg':'查询成功',
            'item':l
        }
    )